package com.a214.yxz.yuejian013.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.a214.yxz.yuejian013.Listener.OrderListener;
import com.a214.yxz.yuejian013.Model.ActivityModel;
import com.a214.yxz.yuejian013.R;


public class ActivityOrderActivity extends AppCompatActivity implements OrderListener,View.OnClickListener{

    private RadioGroup rg;
    private RadioButton WeChat,Alipay;
    private ImageButton add,reduce;
    private TextView name,username,price,tel,intro,allprice;
    private EditText et_num;
    private Button buy;
    private ImageView reg_return;
    private int num=1,userid;
    String acname,acprice,notice,telephone,uname,Pay_type,Aprice,Anum,activity_id,Stunum;
    SharedPreferences sp;
    Context context;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baoming);
        context=ActivityOrderActivity.this;
        initViews();
        reg_return.setOnClickListener(this);
        add.setOnClickListener(this);
        reduce.setOnClickListener(this);
        buy.setOnClickListener(this);
    }

    public void initViews() {
        SharedPreferences user=getSharedPreferences("Userid",0);
        userid=Integer.valueOf(user.getString("user_id",""));
        telephone=sp.getString("user_tel",null);
        uname=sp.getString("user_name",null);
        /*获取活动名*/
        Intent intent=getIntent();
        activity_id=intent.getStringExtra("activity_id");
        acname=intent.getStringExtra("activity_name");
        acprice=intent.getStringExtra("activity_price");
        notice=intent.getStringExtra("activity_notice");

        name=(TextView)findViewById(R.id.name);
        username= (TextView) findViewById(R.id.username);
        price= (TextView) findViewById(R.id.price);
        tel= (TextView) findViewById(R.id.tel);
        intro= (TextView) findViewById(R.id.intro);
        allprice= (TextView) findViewById(R.id.count);
        et_num=(EditText)findViewById(R.id.num) ;
        add=(ImageButton)findViewById(R.id.add);
        reduce=(ImageButton)findViewById(R.id.reduce);
        buy= (Button) findViewById(R.id.buy);
        rg=(RadioGroup) findViewById(R.id.radiogroup);
        WeChat=(RadioButton) findViewById(R.id.W_pay);
        Alipay=(RadioButton) findViewById(R.id.Z_pay);
        reg_return= (ImageView) findViewById(R.id.reg_return);

        name.setText(acname);
        price.setText(acprice);
        intro.setText(notice);
        tel.setText(telephone);
        username.setText(uname);

        allprice.setText((num*Integer.parseInt(acprice))+"(元)");
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.W_pay:
                        Pay_type="微信支付";
                        break;
                    case R.id.Z_pay:
                        Pay_type="支付宝支付";
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){


        /*结算*/
            case R.id.buy:
                count();
                break;


         /*加*/
            case R.id.add:
                doAdd();
                break;
        /*减*/
            case R.id.reduce:
                doReduce();
                break;



            case R.id.reg_return:
                finish();



        }
    }
    private void count(){
        acname = name.getText().toString();
        telephone = tel.getText().toString();
        Anum = et_num.getText().toString();
        Stunum=et_num.getText().toString();
        Aprice=allprice.getText().toString();
        ActivityModel activityModel = new ActivityModel();
        activityModel.signActivity(acname,activity_id,telephone,Anum,Stunum,Pay_type,Aprice,notice,userid,this);
    }

    @Override
    public void onResponse(Integer bean) {
        Toast.makeText(ActivityOrderActivity.this,"下单成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(ActivityOrderActivity.this,"下单失败",Toast.LENGTH_SHORT).show();
    }
    private void doReduce() {

        num=Integer.valueOf(et_num.getText().toString());
        if (0!=num)
            num--;
        et_num.setText(num+"");
        allprice.setText((num*Integer.parseInt(acprice))+"(元)");

    }

    /*加*/
    private void doAdd() {
        num=Integer.valueOf(et_num.getText().toString());
        num++;
        et_num.setText(num+"");
        allprice.setText((num*Integer.parseInt(acprice))+"(元)");
    }
}
